package com.mlz.mlzmall.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.ResponseLogistics;

import java.sql.Date;
import java.util.ArrayList;
import java.util.GregorianCalendar;


/**
 * Created by jack.wang on 2016/1/27.
 */
public class TrackingForLogisticAdapter extends BaseAdapter {
    // 物流数据
    private ArrayList<ResponseLogistics.Data> data;
    // 上下文
    private Context context;
    // item布局
    private int resource;

    public TrackingForLogisticAdapter(Context context, int resource, ArrayList<ResponseLogistics.Data> data) {
        this.context = context;
        this.resource = resource;
        this.data = data;
    }

    @Override
    public int getCount() {
        if (null == data) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(resource, null);
            mViewHolder = new ViewHolder();
            mViewHolder.tvDateOperate = (TextView) convertView.findViewById(R.id.tvDateOperate);
            mViewHolder.tvComment = (TextView) convertView.findViewById(R.id.tvComment);
            mViewHolder.titleLogistics = (TextView)convertView.findViewById(R.id.titleLogistics);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        // 第一条数据显示标题
        if(data.get(position).isfirst()){
            mViewHolder.titleLogistics.setVisibility(View.VISIBLE);
        }else {
            mViewHolder.titleLogistics.setVisibility(View.GONE);
        }
        // 数据更新
        if (null != data) {
            // 更新物流处理时间
            Date dat = new Date(data.get(position).getDateOperate());
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(dat);
            java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            String sb = format.format(gc.getTime());
            mViewHolder.tvDateOperate.setText(sb);
            //更新处理结果
            mViewHolder.tvComment.setText(data.get(position).getComment());
        }

        return convertView;
    }

    class ViewHolder {
        // 物流处理时间
        TextView tvDateOperate;
        // 处理结果
        TextView tvComment;
        // 查看物流标题
       TextView  titleLogistics;
    }


}
